package org.jeecg.modules.sggg.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.sggg.entity.SgHealthyGuidance;
import org.jeecg.modules.sggg.entity.SgTemplateItem;
import org.jeecg.modules.sggg.service.ISgHealthyGuidanceService;
import org.jeecg.modules.sggg.service.ISgTemplateItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 健康指导
 * @Author: jeecg-boot
 * @Date: 2022-11-18
 * @Version: V1.0
 */
@Api(tags = "健康指导")
@RestController
@RequestMapping("/sggg/sgHealthyGuidance")
@Slf4j
public class SgHealthyGuidanceController extends JeecgController<SgHealthyGuidance, ISgHealthyGuidanceService> {
    @Autowired
    private ISgHealthyGuidanceService sgHealthyGuidanceService;

    @Autowired
    private ISgTemplateItemService sgTemplateItemService;

    @ApiOperation(value = "健康指导-分页列表查询", notes = "健康指导-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<SgHealthyGuidance>> queryPageList(SgHealthyGuidance sgHealthyGuidance,
                                                          @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                          @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                          HttpServletRequest req) {
        QueryWrapper<SgHealthyGuidance> queryWrapper = QueryGenerator.initQueryWrapper(sgHealthyGuidance, req.getParameterMap());
        Page<SgHealthyGuidance> page = new Page<>(pageNo, pageSize);
        IPage<SgHealthyGuidance> pageList = sgHealthyGuidanceService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * lx ---- 居民信息 生活方式干预
     *
     * @param mid 居民id
     * @return 数据
     */
    @GetMapping(value = "/queryListByMid")
    public Result<List<SgHealthyGuidance>> queryListByMid(String mid, Integer type, String startTime, String endTime) {
        List<SgHealthyGuidance> sgHealthyGuidanceList = sgHealthyGuidanceService.list(new QueryWrapper<SgHealthyGuidance>()
                .eq("member_id", mid)
                .eq("type", type)
                .ge(startTime != null, "create_time", startTime)
                .le(endTime != null, "create_time", endTime)
                .orderByDesc("create_time"));
        return Result.OK(sgHealthyGuidanceList);
    }

    @AutoLog(value = "健康指导-添加")
    @ApiOperation(value = "健康指导-添加", notes = "健康指导-添加")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody SgHealthyGuidance sgHealthyGuidance) {
        sgHealthyGuidanceService.save(sgHealthyGuidance);
        return Result.OK("添加成功！");
    }

    /**
     * 小程序添加
     *
     * @param sgHealthyGuidance
     * @return
     */
    @PostMapping(value = "/addApplets")
    public Result<String> addApplets(@RequestBody SgHealthyGuidance sgHealthyGuidance) {
        SgTemplateItem sgTemplateItem = sgTemplateItemService.getOne(new QueryWrapper<SgTemplateItem>().eq("id", sgHealthyGuidance.getName()));
        sgHealthyGuidance.setName(sgTemplateItem.getName());
        sgHealthyGuidanceService.save(sgHealthyGuidance);
        return Result.OK("添加成功！");
    }

    @AutoLog(value = "健康指导-编辑")
    @ApiOperation(value = "健康指导-编辑", notes = "健康指导-编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody SgHealthyGuidance sgHealthyGuidance) {
        sgHealthyGuidanceService.updateById(sgHealthyGuidance);
        return Result.OK("编辑成功!");
    }

    @AutoLog(value = "健康指导-通过id删除")
    @ApiOperation(value = "健康指导-通过id删除", notes = "健康指导-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id") String id) {
        sgHealthyGuidanceService.removeById(id);
        return Result.OK("删除成功!");
    }

    @AutoLog(value = "健康指导-批量删除")
    @ApiOperation(value = "健康指导-批量删除", notes = "健康指导-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids") String ids) {
        this.sgHealthyGuidanceService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    @ApiOperation(value = "健康指导-通过id查询", notes = "健康指导-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<SgHealthyGuidance> queryById(@RequestParam(name = "id") String id) {
        SgHealthyGuidance sgHealthyGuidance = sgHealthyGuidanceService.getById(id);
        if (sgHealthyGuidance == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(sgHealthyGuidance);
    }

    /**
     * 导出excel
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, SgHealthyGuidance sgHealthyGuidance) {
        return super.exportXls(request, sgHealthyGuidance, SgHealthyGuidance.class, "健康指导");
    }

    /**
     * 通过excel导入数据
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, SgHealthyGuidance.class);
    }

}
